package com.cunyme;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class Blackboard extends Activity
{
    /** Called when the activity is first created. */
	
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Opening up database again.  These first few lines are the same as in the MainMenu.java, except now we're
        // extracting data from blackboard.
        SQLiteDatabase cuny_meDB = null;
        cuny_meDB = openOrCreateDatabase("cuny_me.db", Context.MODE_PRIVATE, null);
        String[] result_columns = new String[] {"course_id", "course_name"};
        
        // In this page, we are creating a layout of buttons based on the user's class schedule.  Because this is dynamic,
        // we create the layout here, as opposed to in an xml file.
        
        // This creates the main layout...
    	ScrollView parent_layout = new ScrollView(this);
    	parent_layout.setBackgroundColor(0xffffffff);
    	LinearLayout blackboard_layout = new LinearLayout(this);
    	blackboard_layout.setOrientation(LinearLayout.VERTICAL); 
    	
    	// This creates the layout we'll use for each button.
    	LayoutParams bboard_button_params = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
    			ViewGroup.LayoutParams.WRAP_CONTENT); 
        
		parent_layout.addView(blackboard_layout);
    	
    	// This gets the course data
        final Cursor classesCursor = cuny_meDB.query("classes_table", result_columns, null, null, null, null, null);
        classesCursor.moveToFirst();
        
        // For each row, we create a button labeled with the class data.  Currently, I've set it up so it stores the
        // class name as the "TextView," which is the button title, and the course_id in the "Hint," which is technically
        // supposed to be text that shows up if TextView is empty.  It provides valuable storage, though.  Right now, all
        // this code does is produce Toast messages when you click on the buttons, but this provides a framework for what
        // we'll eventually need to do, which is create a "class main menu" activity which has all the options (Announcements,
        // etc.), and then activities for each option.  We can pass the id between activities to determine which class' info
        // we are manipulating.
        for (int i = 0; i < classesCursor.getCount(); i++)
        {
        	String blackboard_class_link = classesCursor.getString(0);
        	String blackboard_class_name = classesCursor.getString(1);
            final Button class_button = new Button(this); 
            class_button.setText(blackboard_class_name);
            class_button.setHint(blackboard_class_link);
            class_button.setLayoutParams(bboard_button_params);
            class_button.setOnClickListener(new OnClickListener()
        	{

				public void onClick(View v) {
					// TODO Auto-generated method stub
					//Toast.makeText(getApplicationContext(), class_button.getText(), Toast.LENGTH_SHORT).show();
					//Toast.makeText(getApplicationContext(), class_button.getHint(), Toast.LENGTH_SHORT).show();
					
					
					Intent gotoBlackboard_main = new Intent(Blackboard.this, Blackboard_main.class);
					gotoBlackboard_main.putExtra("course_id", class_button.getHint());
					startActivity(gotoBlackboard_main);
				}
        	});
            blackboard_layout.addView(class_button);
            classesCursor.moveToNext();
        }
        
        // After each button is created and added to the view, we set the view and close the database.
        setContentView(parent_layout);
        cuny_meDB.close();
    }
}
